package com.rgs.oauth2gateway.pojo.r;


import lombok.Getter;

import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.Objects;

/**
 * @author RenGaoshuai
 * @date 2024/4/11 16:29
 * @description
 */
@Getter
public class Result<T> {

    public static final Integer FAIL_CODE = 1009;
    public static final Integer COMMON_SUCCESSFUL_CODE = 1008;
    public static final Integer POC_SUCCESSFUL_CODE = 200;
    public static final String SUCCESSFUL_MESG = "处理成功";
    public static final String SUCCESS = "success";
    public static final String FALSE = "false";

    private Integer code;
    private String message;
    private Instant time;
    private T data;


    public Result() {
        this.time = ZonedDateTime.now().toInstant();
    }


    /**
     * @param code
     * @param message
     */
    public Result(Integer code, String message) {
        this.code = code;
        this.message = message;
        this.time = ZonedDateTime.now().toInstant();
    }

    /**
     * 内部使用，用于构造成功的结果
     *
     * @param code
     * @param message
     * @param data
     */
    private Result(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.time = ZonedDateTime.now().toInstant();
    }


    /**
     * 快速创建成功结果并返回结果数据
     *
     * @param data
     * @return Result
     */
    public static Result success(Object data) {
        return new Result<>(COMMON_SUCCESSFUL_CODE, SUCCESSFUL_MESG, data);
    }


    /**
     * 快速创建成功结果
     *
     * @return Result
     */
    public static Result success() {
        return success(null);
    }

    /**
     * 系统异常类没有返回数据
     *
     * @return Result
     */
    public static Result fail(Integer code, String message) {
        return new Result(code, message);
    }


    /**
     * 系统异常类没有返回数据
     *
     * @return Result
     */
    public static Result fail(String message) {
        return new Result(FAIL_CODE, message);
    }
    /**
     * 系统异常类没有返回数据
     *
     * @return Result
     */
    public static Result fail() {
        return new Result(FAIL_CODE, null);
    }





    /**
     * 成功code=000000
     *
     * @return true/false
     */
    public boolean isSuccess() {
        return Objects.equals(COMMON_SUCCESSFUL_CODE, this.code) || Objects.equals(POC_SUCCESSFUL_CODE, this.code);
    }

    /**
     * 失败
     *
     * @return true/false
     */
    public boolean isFail() {
        return !isSuccess();
    }

    /**
     * 日志链路id
     */
    private String traceId;

    public void setTraceId(String traceId) {
        this.traceId = traceId;
    }
}

